<template>
<div>
  <!-- 面包屑导航 -->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/AdminIndex' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>系统信息</el-breadcrumb-item>
    <el-breadcrumb-item>商家列表</el-breadcrumb-item>
  </el-breadcrumb>

  <!-- 搜索导航栏 -->
  <div class="con" >
    <div style="float: left">
      <el-button type="success" icon="el-icon-share">导出</el-button>
    </div>

    <div style="float: right">
      <!-- 搜索添加 -->
      <el-input placeholder="请输入工厂名称" clearable v-model="queryInfo.query"  @clear="getFactoryList">
        <el-button slot="append" icon="el-icon-search" @click="getFactoryList"></el-button>
      </el-input>
    </div>
  </div>

  <!-- 信息展示栏 -->
  <div class="con">
    <el-table :data="factoryList" border stripe>
      <el-table-column label="序号"  type="index" width="50px"></el-table-column>
      <el-table-column label="账号" prop="account"></el-table-column>
      <el-table-column label="负责人员" prop="user_name"></el-table-column>
      <el-table-column label="商家名称" prop="f_name"></el-table-column>
      <el-table-column label="商家简介" prop="f_brief"></el-table-column>
      <el-table-column label="商家状态" prop="state">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.state" @change="stateChanged(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- 修改 -->
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row)"></el-button>
          <!-- 删除 -->
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteFactory(scope.row.factory_id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px">
      <!-- 分页 size-change 每页最大数变化 current-change 页数变化 layout 功能组件-->
      <el-pagination
          style="text-align: center; "
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 5, 9, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </div>




  </div>

  <!-- 修改用户对话框 -->
  <el-dialog title="修改用户信息" :visible.sync="editDialogVisible" width="30%" @colse="editDialogClosed">
    <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
      <!-- 用户名 -->
      <el-form-item label="负责人姓名" prop="user_name">
        <el-input v-model="editForm.user_name" disabled></el-input>
      </el-form-item>
      <!-- 姓名 -->
      <el-form-item label="商家名" prop="f_name">
        <el-input v-model="editForm.f_name" ></el-input>
      </el-form-item>
      <!-- 邮箱 -->
      <el-form-item label="简介" prop="f_brief">
        <el-input v-model="editForm.f_brief"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editFactoryInfo">确 定</el-button>
      </span>
  </el-dialog>



</div>




</template>

<script>
export default {
  name: "FactoryList",
  data(){
    return{
      //显示信息
      factoryList:[],
      //分页查询信息
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 9
      },
      //总数目
      total:0,
      //修改框是否可见
      editDialogVisible:false,
      //修改框
      editForm:{
        f_name:'',
        user_name:'',
        f_brief:'',
        factory_id:'',
      },
      editFormRules:{

      }

    }
  },
  created() {
    this.getFactoryList()
  },
  methods:{
    //得到数据
    async getFactoryList(){
      const {data:res} = await this.$http.get("allFactories",{params:this.queryInfo});
      console.log(res)
      this.factoryList=res.data;
      this.total=res.number;
    },
    // 监听pageSize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getFactoryList(); // 数据发生改变重新申请数据
    },
    // 监听pageNum改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getFactoryList(); // 数据发生改变重新申请数据
    },
    //改变工厂状态
    async stateChanged(info) {
      console.log(info)
      const {data: res} = await this.$http.post("facChangeState",info);
      if(res!='success'){
        return this.$message.error(res)
      }else {
        this.$message.success("操作成功")
      }
    },
    //删除
    async deleteFactory(id){
      const confirmResult =await this.$confirm('工厂将从数据库中删除,同时关联的用户信息也会被删除，是否继续？','提示',{
        confirmButtonText:'确定',
        cancelButtonText:'取消',
        type:'warning'
      }).catch(err=>err)
      if(confirmResult!='confirm'){
        return this.$message.info("已取消删除");
      }
      const {data:res} = await this.$http.delete("/deleteFactory?id="+id);
      if(res!='success'){
        return this.$message.error(res)
      }else {
        this.getFactoryList();
        return this.$message.success("删除成功！")
      }
    },

    //修改
    editFactoryInfo(){
      this.$refs.editFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("updateFactory",this.editForm);
        if (res != "success") {
          return this.$message.error(res);
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.editDialogVisible = false;
        this.getFactoryList();
      })
    },
    showEditDialog(user){
      this.editDialogVisible=true
      this.editForm.f_name=user.f_name
      this.editForm.user_name=user.user_name
      this.editForm.f_brief=user.f_brief
      this.editForm.factory_id=user.factory_id
    },
    editDialogClosed(){
      this.$refs.editFormRef.resetFields();// 重置表单项
    }
  }
}
</script>

<style scoped>
.con{
  background-color: #FFFFFF;
  margin: 10px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  padding-left: 10px;
  padding-right: 10px;
  padding-top: 10px;
  padding-bottom: 10px;
  overflow: hidden;
}
</style>